package com.menghanjun.mhjresourcesharing.controller;


import com.menghanjun.mhjresourcesharing.entity.TBar;
import com.menghanjun.mhjresourcesharing.service.ITBarService;
import com.menghanjun.mhjresourcesharing.utils.JsonResponseBody;
import com.menghanjun.mhjresourcesharing.utils.JsonResponseStatus;
import com.menghanjun.mhjresourcesharing.vo.TBarVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author menghanjun
 * @since 2024-05-26
 */
@RestController
@RequestMapping("/bar")
@Slf4j
public class TBarController {

    @Autowired
    private ITBarService BarService;

    /**
     * 初始化导航栏
     * @return
     */
    @GetMapping("/initNav")
    public JsonResponseBody<?> initNav(){
        try {
            List<TBar> navList = BarService.getAllNavList();
            return new JsonResponseBody(navList);
        } catch (Exception e) {
            log.error("初始化导航栏失败",e.getMessage());
            return new JsonResponseBody(JsonResponseStatus.ERORR);
        }
    }


    /**
     * 初始化类型下拉框
     * @return
     */
    @GetMapping("/initType")
    public JsonResponseBody<?> initType(){
        try {
            List<TBar> navList = BarService.getAllTypeSelectList();
            return new JsonResponseBody(navList);
        } catch (Exception e) {
            log.error("初始化类型下拉框",e.getMessage());
            return new JsonResponseBody(JsonResponseStatus.ERORR);
        }
    }


    /**
     * 获取字典列表
     * @param bar
     * @return
     */
    @GetMapping("/admin/getBarList")
    public JsonResponseBody<?> getBarList(TBar bar){
        try {
            List<TBarVo> barList = BarService.getBarList(bar);
            return new JsonResponseBody(barList);
        } catch (Exception e) {
            log.error("获取导航栏失败",e.getMessage());
            return new JsonResponseBody(JsonResponseStatus.ERORR);
        }
    }


    /**
     * 添加字典
     * @param bar
     * @return
     */
    @PostMapping("/admin/addBar")
    public JsonResponseBody<?> addBar(@RequestBody TBar bar){
        try {
            BarService.save(bar);
            return new JsonResponseBody(JsonResponseStatus.SUCCESS);
        } catch (Exception e) {
            log.error("添加导航栏失败",e.getMessage());
            return new JsonResponseBody(JsonResponseStatus.ERORR);
        }
    }



    /**
     * 修改字典
     * @param bar
     * @return
     */
    @PutMapping("/admin/updateBar")
    public JsonResponseBody<?> updateBar(@RequestBody TBar bar){
        try {
            BarService.updateBar(bar);
            return new JsonResponseBody(JsonResponseStatus.SUCCESS);
        } catch (Exception e) {
            log.error("修改导航栏失败",e.getMessage());
            return new JsonResponseBody(JsonResponseStatus.ERORR);
        }
    }


    /**
     * 删除字典
     * @param bar
     * @return
     */
    @DeleteMapping("/admin/deleteBar")
    public JsonResponseBody<?> deleteBar(@RequestBody TBar bar){
        try {
            BarService.removeById(bar);
            return new JsonResponseBody(JsonResponseStatus.SUCCESS);
        } catch (Exception e) {
            log.error("删除导航栏失败",e.getMessage());
            return new JsonResponseBody(JsonResponseStatus.ERORR);
        }
    }


}
